<script setup lang="ts">
import { reactive, ref } from "vue";
import { onLoad, onUnload } from "@dcloudio/uni-app";
import { usePageScroll } from "@/hooks/scroll";
import {
  order_merge_info_api,
  offline_course_order_mini_pay_api,
  live_course_mini_pay_api,
} from "@/package/my/api/guider_home";
import { pay_api } from "@/package/my/api/pass_port_order";
import { pay_api as pay_api2 } from "@/package/my/api/mini_app_tour_pay";
import { EV_ORDER } from "@/constant/event";
import { Account } from "@/utils/account";
import { requestPayment } from "@/utils/payment";
import { strReplaceByLen } from "@/utils/string";
import { Router } from "@/utils/router";
import { Toast } from "@/utils/toast";

import NavBack from "@/components/nav/back.vue";
import NavBar from "@/components/nav/bar.vue";
import NavBg from "@/components/nav/bg.vue";
import NavSpace from "@/components/nav/space.vue";

onLoad((options) => {
  Account.checkLogin((isLogin) => {
    if (isLogin) Router.navigateBack();
    tempData.id = options?.id;
    infoData.order_list_type = options?.type;
    reloadData();

    uni.$on(EV_ORDER, evOrderCallback);
  });
});
onUnload(() => uni.$off(EV_ORDER, evOrderCallback));

const { pageIsScroll, onPageScroll } = usePageScroll();

const reloadData = () => {
  return order_merge_info_api({
    order_id: tempData.id,
    order_list_type: infoData.order_list_type,
  }).then((resp) => {
    infoData.cover_pic =
      resp.cover_pic || resp.horizontal_pic || resp.pic_url || resp.cover_img;
    infoData.product_name = resp.product_name || resp.title;
    infoData.unit_price = resp.unit_price;
    infoData.order_num = resp.order_num;
    infoData.specs =
      resp.specs ||
      resp.product_type ||
      (resp.end_date || resp.valid_end_date
        ? `有效期：${resp.end_date || resp.valid_end_date}`
        : "");
    infoData.sale_price = resp.sale_price;
    infoData.pay_price = resp.pay_price;
    infoData.buyer_name = resp.buyer_name;
    infoData.buyer_phone = strReplaceByLen(resp.buyer_phone || "");
    infoData.buyer_address = resp.buyer_address;
    infoData.desc = resp.desc;
    infoData.travel_user_info = (resp.travel_user_info || []).map((i: any) => {
      return {
        ...i,
        phone: strReplaceByLen(i.phone),
        identity: strReplaceByLen(i.identity, 6),
      };
    });
    infoData.id_card = strReplaceByLen(resp.id_card || "", 6);
    infoData.trip_date = resp.trip_date;
    infoData.out_trade_no = resp.out_trade_no;
    infoData.cdate = resp.cdate;
    infoData.pay_status = resp.pay_status;
    infoData.pay_time = resp.pay_time;
    infoData.cancel_date = resp.cancel_date;
    infoData.send_shop_time = resp.send_shop_time;
    infoData.ship_status = resp.ship_status;
    infoData.express_name = resp.express_name;
    infoData.express_out_trade_no = resp.express_out_trade_no;
    infoData.finish_shop_time = resp.finish_shop_time;
  });
};

const infoData = reactive({
  order_list_type: "",
  cover_pic: "",
  product_name: "",
  unit_price: "",
  order_num: "",
  specs: "",
  sale_price: "",
  pay_price: "",
  buyer_name: "",
  buyer_phone: "",
  buyer_address: "",
  desc: "",
  travel_user_info: [] as any[],
  id_card: "",
  trip_date: "",
  out_trade_no: "",
  cdate: "",
  pay_status: "",
  pay_time: "",
  cancel_date: "",
  send_shop_time: "",
  ship_status: "",
  express_name: "",
  express_out_trade_no: "",
  finish_shop_time: "",
});

const payClick = () => {
  Toast.showLoading();
  if (infoData.order_list_type === "1") {
    pay_api({ order_id: tempData.id })
      .then((resp) => {
        Toast.hideLoading();
        requestPayment(resp, () => {
          uni.$emit(EV_ORDER, {
            type: "pay",
            data: { type: "goods", id: tempData.id },
          });
        });
      })
      .catch(() => Toast.hideLoading());
  } else if (infoData.order_list_type === "2") {
    offline_course_order_mini_pay_api({ order_id: tempData.id })
      .then((resp) => {
        Toast.hideLoading();
        requestPayment(resp, () => {
          uni.$emit(EV_ORDER, {
            type: "pay",
            data: { type: "train", id: tempData.id },
          });
        });
      })
      .catch(() => Toast.hideLoading());
  } else if (infoData.order_list_type === "4") {
    live_course_mini_pay_api({ order_id: tempData.id })
      .then((resp) => {
        Toast.hideLoading();
        requestPayment(resp, () => {
          uni.$emit(EV_ORDER, {
            type: "pay",
            data: { type: "live", id: tempData.id },
          });
        });
      })
      .catch(() => Toast.hideLoading());
  } else {
    pay_api2({ pre_order_id: tempData.id })
      .then((resp) => {
        Toast.hideLoading();
        requestPayment(resp, () => {
          uni.$emit(EV_ORDER, {
            type: "pay",
            data: { type: "jps", id: tempData.id },
          });
        });
      })
      .catch(() => Toast.hideLoading());
  }
};
const copyClick = (text: string) => {
  uni.setClipboardData({ data: infoData.express_out_trade_no });
};

const evOrderCallback = (e: { type: ""; data: { type: "" } }) => {
  console.info(`触发事件：${EV_ORDER}`, e);
  reloadData();
};

const tempData = { id: "" };
</script>

<template>
  <NavBack />
  <NavBar title="订单详情" />
  <NavBg :retract="pageIsScroll" url="/package/my/static/order-detail-bg.png" />
  <NavSpace />

  <view class="detail-info">
    <view style="display: flex; align-items: flex-start">
      <image
        class="detail-info-img"
        :src="infoData.cover_pic"
        mode="aspectFill"
      />
      <view class="detail-info-name">{{ infoData.product_name }}</view>
      <view style="flex-shrink: 0">
        <view class="detail-info-price">
          <text class="detail-info-price2">￥</text>{{ infoData.unit_price }}
        </view>
        <view
          class="detail-info-num"
          v-if="
            infoData.order_list_type === '1' ||
            infoData.order_list_type === '5' ||
            infoData.order_list_type === '6'
          "
        >
          x{{ infoData.order_num }}
        </view>
      </view>
    </view>
    <view
      class="detail-info-spec"
      v-if="
        infoData.order_list_type === '1' ||
        infoData.order_list_type === '5' ||
        infoData.order_list_type === '6'
      "
    >
      {{ infoData.specs }}
    </view>
    <view class="detail-info-line"></view>

    <view class="detail-data">
      <view class="detail-data-item">
        <view class="detail-data-label">订单总价：</view>
        <view class="detail-info-price3">
          <text class="detail-info-price4">￥</text>{{ infoData.sale_price }}
        </view>
      </view>
      <view class="detail-data-item">
        <view class="detail-data-label">实付金额：</view>
        <view class="detail-info-price5">
          <text class="detail-info-price6">￥</text>{{ infoData.pay_price }}
        </view>
      </view>
    </view>
  </view>

  <view class="detail-content" v-if="infoData.order_list_type === '1'">
    <view class="detail-data">
      <view class="detail-data-item">
        <view class="detail-data-label">联系人</view>
        <view class="detail-data-value">{{ infoData.buyer_name }}</view>
      </view>
      <view class="detail-data-item">
        <view class="detail-data-label">电话号码</view>
        <view class="detail-data-value">{{ infoData.buyer_phone }}</view>
      </view>
      <view class="detail-data-item">
        <view class="detail-data-label">收货地址</view>
        <view class="detail-data-value">{{ infoData.buyer_address }}</view>
      </view>
      <view class="detail-data-item">
        <view class="detail-data-label">备注</view>
        <view class="detail-data-value">{{ infoData.desc }}</view>
      </view>
    </view>
  </view>
  <view class="detail-content" v-else-if="infoData.order_list_type === '2'">
    <view class="detail-data">
      <view class="detail-data-item">
        <view class="detail-data-label">姓名</view>
        <view class="detail-data-value">{{ infoData.buyer_name }}</view>
      </view>
      <view class="detail-data-item">
        <view class="detail-data-label">身份证号</view>
        <view class="detail-data-value">{{ infoData.id_card }}</view>
      </view>
      <view class="detail-data-item">
        <view class="detail-data-label">联系方式</view>
        <view class="detail-data-value">{{ infoData.buyer_phone }}</view>
      </view>
    </view>
  </view>
  <view
    class="detail-content"
    v-else-if="
      infoData.order_list_type === '5' || infoData.order_list_type === '6'
    "
  >
    <view class="detail-data">
      <view class="detail-data-item">
        <view class="detail-data-label">联系人</view>
        <view class="detail-data-value">{{ infoData.buyer_name }}</view>
      </view>
      <view class="detail-data-item">
        <view class="detail-data-label">电话号码</view>
        <view class="detail-data-value">{{ infoData.buyer_phone }}</view>
      </view>

      <template v-for="(i, d) of infoData.travel_user_info" :key="d">
        <view class="detail-data-item">
          <view class="detail-data-label">出行人{{ d + 1 }}</view>
          <view class="detail-data-value">{{ i.name }}</view>
        </view>
        <view class="detail-data-item">
          <view class="detail-data-label">电话号码</view>
          <view class="detail-data-value">{{ i.phone }}</view>
        </view>
        <view class="detail-data-item">
          <view class="detail-data-label">身份证号码</view>
          <view class="detail-data-value">{{ i.identity }}</view>
        </view>
        <view class="detail-data-item-line"></view>
      </template>
    </view>
  </view>

  <view class="detail-content">
    <view class="detail-data">
      <view class="detail-data-item">
        <view class="detail-data-label">产品类型</view>
        <view class="detail-data-value">
          {{
            infoData.order_list_type === "1"
              ? "商品"
              : infoData.order_list_type === "2"
              ? "线下课程"
              : infoData.order_list_type === "5"
              ? "人工讲解"
              : infoData.order_list_type === "6"
              ? "文博讲解"
              : "直播课"
          }}
        </view>
      </view>
      <view
        class="detail-data-item"
        v-if="
          infoData.order_list_type === '5' || infoData.order_list_type === '6'
        "
      >
        <view class="detail-data-label">出行日期</view>
        <view class="detail-data-value">{{ infoData.trip_date }}</view>
      </view>
      <view class="detail-data-item">
        <view class="detail-data-label">订单编号</view>
        <view class="detail-data-value">{{ infoData.out_trade_no }}</view>
      </view>
      <view class="detail-data-item">
        <view class="detail-data-label">下单时间</view>
        <view class="detail-data-value">{{ infoData.cdate }}</view>
      </view>
      <view class="detail-data-item" v-if="infoData.pay_status === 1">
        <view class="detail-data-label">支付时间</view>
        <view class="detail-data-value">{{ infoData.pay_time }}</view>
      </view>
      <view class="detail-data-item" v-else-if="infoData.pay_status === 2">
        <view class="detail-data-label">取消时间</view>
        <view class="detail-data-value">{{ infoData.cancel_date }}</view>
      </view>
    </view>
  </view>

  <view class="detail-content" v-if="infoData.order_list_type === '1'">
    <view class="detail-data">
      <view class="detail-data-item" v-if="infoData.send_shop_time">
        <view class="detail-data-label">发货时间</view>
        <view class="detail-data-value">{{ infoData.send_shop_time }}</view>
      </view>
      <view class="detail-data-item" v-if="infoData.ship_status === 20">
        <view class="detail-data-label">配送方式</view>
        <view class="detail-data-value">
          {{ `${infoData.express_name} ${infoData.express_out_trade_no}` }}
        </view>
        <view class="detail-data-btn" @click="copyClick">复制单号</view>
      </view>
      <view class="detail-data-item" v-if="infoData.finish_shop_time">
        <view class="detail-data-label">完成时间</view>
        <view class="detail-data-value">{{ infoData.finish_shop_time }}</view>
      </view>
    </view>
  </view>

  <view class="detail-menu">
    <template v-if="infoData.pay_status === 0">
      <view class="detail-menu-tip">请30分钟内完成支付</view>
      <view class="detail-menu-btn" @click="payClick">去支付</view>
    </template>
    <view class="detail-menu-btn2" v-else-if="infoData.pay_status === 1">
      已付款
    </view>
    <view class="detail-menu-btn2" v-else>
      {{ infoData.pay_status === 3 ? "已退款" : "超时未支付" }}
    </view>
  </view>
</template>

<style lang="scss">
page {
  padding-top: 30rpx;
}

.detail-info {
  position: relative;
  padding-top: 20rpx;
  margin: 0 30rpx 20rpx;
  border-radius: 20rpx;
  background-color: #ffffff;

  .detail-info-img {
    flex-shrink: 0;
    width: 190rpx;
    height: 128rpx;
    margin-left: 30rpx;
    border-radius: 20rpx;
  }

  .detail-info-name {
    flex-grow: 1;
    min-width: 0;
    height: 80rpx;
    margin: 0 20rpx;
    font-size: 28rpx;
    color: #333333;
    line-height: 40rpx;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    text-overflow: ellipsis;
    overflow: hidden;
  }

  .detail-info-price {
    padding-right: 20rpx;
    font-size: 36rpx;
    color: #333333;
    line-height: 50rpx;
    text-align: right;
  }

  .detail-info-price2 {
    font-size: 24rpx;
    line-height: 34rpx;
  }

  .detail-info-num {
    padding-right: 20rpx;
    font-size: 28rpx;
    color: #666666;
    line-height: 40rpx;
    text-align: right;
  }

  .detail-info-spec {
    margin: 20rpx 30rpx 0;
    font-size: 24rpx;
    color: #999999;
    line-height: 34rpx;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }

  .detail-info-line {
    margin: 30rpx 40rpx 0;
    border-bottom: 2rpx dashed #ebebeb;
  }

  .detail-info-price3 {
    margin-left: auto;
    font-size: 36rpx;
    color: #333333;
    line-height: 50rpx;
  }

  .detail-info-price4 {
    font-size: 28rpx;
    line-height: 40rpx;
  }

  .detail-info-price5 {
    margin-left: auto;
    font-size: 48rpx;
    color: #ee5734;
    line-height: 66rpx;
    font-weight: bold;
  }

  .detail-info-price6 {
    font-size: 32rpx;
    line-height: 44rpx;
  }
}

.detail-data {
  .detail-data-item {
    display: flex;
    align-items: center;
    min-height: 100rpx;
    padding: 0 30rpx;
  }

  .detail-data-label {
    flex-shrink: 0;
    font-size: 28rpx;
    color: #999999;
  }

  .detail-data-value {
    flex-grow: 1;
    min-width: 0;
    padding: 30rpx 0;
    margin-left: 20rpx;
    font-size: 30rpx;
    color: #333333;
    line-height: 40rpx;
    text-align: right;
  }

  .detail-data-btn {
    flex-shrink: 0;
    padding: 0 14rpx;
    margin-left: 20rpx;
    font-size: 24rpx;
    color: #666666;
    line-height: 38rpx;
    border-radius: 26rpx;
    border: 2rpx solid #c5c5c5;
    background-color: #f1f1f1;
  }
}

.detail-content {
  margin: 0 30rpx 20rpx;
  border-radius: 20rpx;
  background-color: #ffffff;
}

.detail-menu {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 20rpx 30rpx 40rpx;

  .detail-menu-tip {
    font-size: 24rpx;
    color: #999999;
  }

  .detail-menu-btn,
  .detail-menu-btn2 {
    padding: 0 28rpx;
    margin-left: 20rpx;
    font-size: 26rpx;
    color: #ffffff;
    line-height: 56rpx;
    border-radius: 30rpx;
    border: 2rpx solid #ff7900;
    background-color: #ff7900;
  }

  .detail-menu-btn2 {
    border-color: #dddddd;
    background-color: #dddddd;
  }
}
</style>